/*
 * Copyright 2007 Rob Nielsen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.proxymatic.core.handlers.method;

import com.googlecode.proxymatic.core.AbstractMethodHandler;
import com.googlecode.proxymatic.core.BuildtimeContext;
import com.googlecode.proxymatic.core.MethodInvoker;
import com.googlecode.proxymatic.core.ProxyBuilder;
import com.googlecode.proxymatic.core.ProxyConfiguration;
import com.googlecode.proxymatic.core.RuntimeContext;
import com.googlecode.proxymatic.core.util.ReflectUtil;

import java.io.Serializable;
import java.lang.reflect.Method;

public class EqualsAndHashCodeMethodHandler extends AbstractMethodHandler {
    private static final Method EQUALS_METHOD = ReflectUtil.getMethod(Object.class, "equals", Object.class);
    private static final Method HASHCODE_METHOD = ReflectUtil.getMethod(Object.class, "hashCode");

    public MethodInvoker getInvoker(Method method, BuildtimeContext context, final ProxyBuilder proxyBuilder) {
        if (method.equals(EQUALS_METHOD)) {
            return new EqualsMethodInvoker(proxyBuilder);
        } else if (method.equals(HASHCODE_METHOD)) {
            return new HashCodeMethodInvoker(proxyBuilder.hashCode());
        }
        return NOT_HANDLED;
    }

    private class EqualsMethodInvoker implements MethodInvoker, Serializable {
        private final ProxyBuilder proxyBuilder;

        public EqualsMethodInvoker(final ProxyBuilder proxyBuilder) {
            this.proxyBuilder = proxyBuilder;
        }

        public Object invoke(Method method, Object[] parameters, RuntimeContext context) throws Throwable {
            Object objectToCompare = parameters[0];
            ProxyConfiguration configuration = proxyBuilder.getProxyEngine().getProxyConfiguration(objectToCompare);
            if (configuration != null && configuration.getRuntimeContext().equals(context)) {
                return Boolean.valueOf(proxyBuilder.equals(configuration.getProxyBuilder()));
            }
            return Boolean.FALSE;
        }

        public Class[] getExceptionTypes() {
            return NO_DECLARED_EXCEPTIONS;
        }
    }

    private class HashCodeMethodInvoker implements MethodInvoker, Serializable {
        private int proxyBuilderHashCode;

        public HashCodeMethodInvoker(int proxyBuilderHashCode) {
            this.proxyBuilderHashCode = proxyBuilderHashCode;
        }

        public Object invoke(Method method, Object[] parameters, RuntimeContext context) throws Throwable {
            return new Integer(31 * proxyBuilderHashCode + context.hashCode());
        }

        public Class[] getExceptionTypes() {
            return NO_DECLARED_EXCEPTIONS;
        }
    }
}
